//Do file that calculates the number of movers and stayers at each election, and
//present descriptive statistics separately for movers and stayers.
// The results are presented in Figures A1 and Tables A2 and A3.

// Last changed: 2020-03-25

 clear all
  set more off 
  set matsize 10000
  
  capture log close
  local output "C:\Userdata\Shared\Dofiles\DoAnalysis\PlacementCandidacy"
  log using "`output'\Output\utp-FigureA1_TabA2_A3", replace text
  
  use "E:\ProjData\PlacementCandidacy\utp-DataMainSample.dta", clear

  local sample "inrange(InvAr, 1987, 1991) & inrange(InvAlder, 18, 55) & inrange(Ar, 1991, 2014) & FlyktInv == 1 & Rostratt == 1"
  local controls1 "InvAr IFAUkod Ar FodelseAr"
  local controls2 "Kvinna Gift_InvAr UtbAr_InvAr Barn0_15_InvAr"
  local controls3 "LoneInk UtbAr Sysselsatt" 
  listwise Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr TidI InvAlder logpEnklavVuxenInvAr
  
  reghdfe Nominerad logpEnklavVuxen `controls2' if `sample', absorb(KommunValAr `controls1') cluster(KommunValAr) old
  
  keep if e(sample)==1
  sum
  
	gen Mover = KommunValAr != InKommun if KommunValAr<. & InKommun<.
	
	di "************************Movers and Stayers in 1991*********************************"
	tab Mover if Ar==1991 
	
	di "************************Movers and Stayers in 1994*********************************"
	tab Mover if Ar==1994 
	
	di "************************Movers and Stayers in 1998*********************************"
	tab Mover if Ar==1998 

	di "************************Movers and Stayers in 2002*********************************"
	tab Mover if Ar==2002 
	
	di "************************Movers and Stayers in 2006*********************************"
	tab Mover if Ar==2006 
	
	di "************************Movers and Stayers in 2010*********************************"
	tab Mover if Ar==2010 
	
	di "************************Movers and Stayers in 2014*********************************"
	tab Mover if Ar==2014 
	
	bysort LopNr: egen mMover = max(Mover)
    
	bysort Ar: egen AndFlytt = mean(Mover)
	bysort Ar: gen id = _n
	gen AndKvar = 1-AndFlytt
	
	twoway (scatter AndKvar Ar if id == 1, color(black) ylabels(.2(.1).8) yscale(range(0.2 .8)) ytitle("Share Stayers") xtitle("Year")) ///
	       (line AndKvar Ar if id == 1, lcolor(black)), legend(off) graphregion(color(white))
	graph export "`output'\Output\FigureA1.png", replace
	tab mMover 
	
di "Descriptive statistics in 2014 for all in the sample"
  estpost tabstat Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr if Ar ==2014 , column(stat) statistics(mean sd)
  eststo md1

di "Descriptive statistics in 2014 for those who never moved"
  estpost tabstat Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr if Ar ==2014 & Mover==0, column(stat) statistics(mean sd)
  eststo md2

di "Descriptive statistics in 2014 for those who moved before 2014"	
  estpost tabstat Alder Kvinna Gift_InvAr Barn0_15_InvAr UtbAr_InvAr logpEnklavInvAr Nominerad Vald  InvAlder  InvAr if Ar ==2014 & Mover==1, column(stat) statistics(mean sd)
  eststo md3

  esttab md1 md2 md3 using "`output'\Output\TableA3.rtf", main(mean) aux(sd) nostar unstack nonote nomtitle b(2) ///
				rename(Alder Age Kvinna Female Gift_InvAr Married Barn0_15_InvAr "Nr of children under 16" ///
				 UtbAr_InvAr "Years of education" Nominerad Nominated(%) Vald Elected(%) ///
				InvAlder "Age at immigration" InvAr "Immigration year" logpEnklavInvAr "Ethnic density (log)") replace
				
// Tabulate the frequencies for country of birth in the sample
  eststo clear	
 
  capture drop id
  gsort LopNr -Ar
  bysort LopNr: gen id = _n
  
  codebook LopNr, compact
  estpost tabulate IFAUkod if id==1
  esttab using "`output'\Output\TableA2.rtf", cells("b(label(freq)) pct(fmt(2))") replace

log close
